<template>
	<view v-if="list">
		<view class="tab-bar" :style="{ backgroundColor: backgroundColor }">
			<view class="tabbar-border"></view>
			<view class="item" v-for="(item, index) in list" :key="index"
				@click="redirectTo(item.link)">
				<view class="bd">
					<view class="icon">
						<image :src="verify(item.link) ? item.selectedIconPath : item.iconPath" />
						<view v-if="item.cartNum != 0" class="cartNumberBtn font-size-activity-tag color-base-bg"
							>{{ item.cartNum }}
						</view>
					</view>
					<view class="label"
						:style="{ color: verify(item.link) ? textHoverColor : textColor }">{{ item.text }}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'tabbar',
		props: {
			value: {
				type: Object
			},
			type: {
				type: String,
				default: ''
			},
			siteId: {
				type: [Number, String],
				default: 0
			}
		},

		data() {
			return {
				currentRoute: '', //当前页面路径
				//特殊页面处理悬浮
				// special: ['/pages/live/live_video_player_m'],
				// specialPage: '',
				isIphoneX: false, //判断手机是否是iphoneX以上
				jumpFlag: true, //是否可以跳转，防止重复点击

				bottomNav: {},
				textHoverColor: '#004fef',
				textColor: '#333',
				backgroundColor: '',
				list: [
					{
						text:'家长助手',
						link: '/pages/parentAssistant/index',
						iconPath: '../../static/jazs.png',
						selectedIconPath: '../../static/jazs.png',
						cartNum: 0
					},
					{
						text: "计划中心",
						link: "/pages/planCenter/index",
						iconPath: '../../static/jazs.png',
						selectedIconPath:'../../static/jazs.png',
						cartNum: 1
						
					},
					{
						text: "",
						link: "/pages/index/index",
						iconPath: "../../static/middle.png",
						selectedIconPath: "../../static/middle.png",
						cartNum: 0
						
					},
					{
						text: "星辰课堂",
						link: "/pages/xingchen/xingchen",
						iconPath: '../../static/jazs.png',
						selectedIconPath: '../../static/jazs.png',
						cartNum: 25
						
					},
					{
						text:'我的',
						link: '/pages/mine/index',
						iconPath: '../../static/jazs.png',
						selectedIconPath: '../../static/jazs.png',
						cartNum: 0
					}
				],
				bottomType: 1
			};
		},
		mounted() {
			let currentPage = getCurrentPages()[getCurrentPages().length - 1];
			this.currentRoute = currentPage.route;
			// this.getBottomNav();
			// this.$store.dispatch('getCartNumber');
		},
		computed: {
			// cartNumber() {
			// 	return this.$store.state.cartNumber;
			// },
			// isBulge() {
			// 	// 暂时去掉悬浮功能
			// 	return false;
			// 	if (this.bottomNav.list) {
			// 		return this.bottomNav.list.length == 5 && this.special.indexOf(this.currentRoute) == -1 && this
			// 			.bottomNav.bulge;
			// 	} else {
			// 		return false;
			// 	}
			// }
		},
		methods: {
			redirectTo(link) {
				this.$emit('callback');
				if (!this.jumpFlag) return;
				this.jumpFlag = false;
				setTimeout(() => {
					this.jumpFlag = true;
				}, 300);
				// if (link == null || link == '' || !link.wap_url) return false;
				if(link == "/pages/xingchen/xingchen"){
					uni.showToast({
						title: "开发中",
						icon: 'none'
					})
					return
				}
				if (link.indexOf(this.currentRoute) != -1) return false;

				let jump = true;
				let arr = getCurrentPages().reverse();
				// console.log(arr,'arr')
				for (let i = 0; i < arr.length; i++) {
					if (link.indexOf(arr[i].route) != -1) {
						jump = false;
						uni.navigateBack({
							delta: i
						});
						break;
					}
				}
				if (jump) {
					// if (link.indexOf('site_id') == -1) link += '?site_id=' + this.siteId;
					// this.$util.redirectTo(link.wap_url);
					uni.switchTab({
						url: link
					})
				}
			},
			verify(link) {
				// if (link == null || link == '' || !link.link) return false;
				if (link.indexOf(this.currentRoute) != -1) {
					return true;
				}
				return false;
			},
			getBottomNav() {
				let url = '/api/diyview/bottomNav';
				let data = {};
				if (this.siteId) {
					url = '/api/diyview/shopBottomNav';
					data.site_id = this.siteId;
				}

				this.$api.sendRequest({
					url: url,
					data: data,
					success: res => {
						let data = res.data;
						if (data && data.value && data.value.length) {
							var value = JSON.parse(data.value);
							this.bottomNav = value;
							this.textColor = value.textColor;
							this.textHoverColor = value.textHoverColor;
							this.backgroundColor = value.backgroundColor;
							this.bottomType = value.type;
							this.list = value.list;
							this.$store.commit('setTabbarList', JSON.parse(data.value));
							this.$forceUpdate();
						}
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	// .tab-bar .item:nth-child(3) .bd {
	// 	margin-top: -100rpx;
	// 	height: 300rpx;
	// }
	// .tab-bar .item:nth-child(3) .bd .icon{
	// 	width: 100rpx;
	// 	height: 100rpx;
	// }
	.tab-bar .item:nth-child(4) .bd {
		margin-top: -40rpx;
		height: 300rpx;
	}
	.tab-bar .item:nth-child(4) .bd .icon{
		width: 100rpx;
		height: 100rpx;
	}
	.placeholder {
		height: 56px;

		&.bluge {
			height: 90px;
		}
	}

	.safe-area {
		padding-bottom: 0;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
	}

	.tab-bar {
		background: #ffffff;
		background-color: rgb(255, 255, 255);
		box-sizing: border-box;
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		z-index: 998;
		display: flex;
		border-top: 1px solid #f5f5f5;
		padding-bottom: 0;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		border-radius: 40rpx 40rpx 0 0;
		height: 160rpx;
		background-color: #fff;
		box-shadow: 0px -2px 8px 0px rgba(0,0,0,0.11);

		.tabbar-border {
			background-color: rgba(255, 255, 255, 0.329412);
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 1px;
			-webkit-transform: scaleY(0.5);
			transform: scaleY(0.5);
		}
		
		.item {
			display: flex;
			align-items: center;
			-webkit-box-orient: vertical;
			-webkit-box-direction: normal;
			flex: 1;
			flex-direction: column;
			padding-bottom: 5px;
			box-sizing: border-box;

			.bd {
				position: relative;
				height: 50px;
				flex-direction: column;
				text-align: center;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;

				.icon {
					position: relative;
					display: inline-block;
					margin-top: 5px;
					// width: 40rpx;
					// height: 40rpx;
					width: 60rpx;
					height: 51rpx;
					

					image {
						width: 100%;
						height: 100%;
					}
				}

				.icon1 {
					position: relative;
					display: inline-block;
					margin-top: 5px;
					width: 40rpx;
					height: 40rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.label {
					position: relative;
					text-align: center;
					font-size: 12px;
					line-height: 1;
					margin-top: 12rpx;
				}
			}

			&.bulge {
				.bd {
					position: relative;
					height: 50px;
					flex-direction: column;
					text-align: center;

					.icon {
						margin-top: -30px;
						margin-bottom: 2px;
						border-radius: 50%;
						width: 50px;
						height: 51px;
						padding: 5px;
						border-top: 1px solid #f5f5f5;
						background-color: #fff;
						box-sizing: border-box;

						image {
							width: 100%;
							height: 100%;
							border-radius: 50%;
						}
					}

					.label {
						position: relative;
						text-align: center;
						font-size: 12px;
						line-height: 1.6;
						height: 20px;
						line-height: 20px;
					}
				}
			}

			.cartNumberBtn {
				position: absolute;
				top: -8rpx;
				right: -18rpx;
				width: 24rpx;
				height: 24rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				color: #fff;
				padding: 6rpx;
				border-radius: 50%;
				z-index: 99;
				width: 16rpx;
				height: 16rpx;
				background-color: #fe6063;
				font-size: 18rpx;
			}
		}
	}
</style>
